// JavaScript Document
var message = '';

function MM_popupMsg(msg) { //v1.0
  alert(msg);
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function DoNav(theUrl)
{
  document.location.href = theUrl;
}

function backlink() {
	this.text = 'go back to search results';
	this.type = 'link';
	this.write = backlink_write;
	this.form = true;
}


// write method
function backlink_write() {
	if (! window.history) return;
	if (window.history.length == 0)return;

	this.type = this.type.toLowerCase();
	if (this.type == 'button') {
		if (this.form)
			document.write('<form>');
		document.write('<input type=button onClick="history.back(-1)" VALUE="', this.text, '"');
		if (this.otheratts) document.write(' ', this.otheratts);
		document.write('>');
		if (this.form)document.write('<\/FORM>');
	} else {
		document.write('<a href="javascript:history.back(-1)"');
		if (this.otheratts)
			document.write(' ', this.otheratts);
		document.write('>');
		if (this.type == 'image' || this.type == 'img') {
			document.write('<img src="', this.src, '" alt="', this.text, '"');
			if (this.width) document.write(' width=', this.width);
			if (this.height) document.write(' height=', this.height);
			if (this.otherimgatts) document.write(' ', this.otherimgatts);
			document.write(' border=0 />');
		}
		else
			document.write(this.text);
		document.write('<\/A>');
	}
}	
//JQuery document ready wrapper
$(document).ready(function() {
	
	
	// Date Picker for events
	$(function() {
		var dates = $( "#eventDate, #eventEndDate" ).datepicker({
			defaultDate: "+1w",
			changeMonth: false,
			numberOfMonths: 1,
			dateFormat: 'yy-mm-dd',
			onSelect: function( selectedDate ) {
				var option = this.id == "eventDate" ? "minDate" : "maxDate",
					instance = $( this ).data( "datepicker" ),
					date = $.datepicker.parseDate(
						instance.settings.dateFormat ||
						$.datepicker._defaults.dateFormat,
						selectedDate, instance.settings );
				dates.not( this ).datepicker( "option", option, date );
			}
		});
	});
	// Help slider
	$('#helpLink').click(function() {
 	 $('#helpText').slideToggle('100', function() {
    	// Animation complete.
		//set php session variable used to remember state of toggle while browser open
		 if( $('#helpText').is(":visible") ) {
			$.cookie( 'helpTextCookie' , 'open' );
				}
			else {
			 $.cookie( 'helpTextCookie' , 'closed' );
			}
  	});
	});
	//Event Ajax
	$(".eventRow").hoverIntent(function() {
		$("#eventPreview").load("events_array.php",
		{ 'id': this.id});  
	  $("#eventPreview").animate({opacity: "show"}, "fast");
	  }, function() {
	  $("#eventPreview").animate({opacity: "hide"}, "fast");
	});
	//replace file funtion
	$('#changeFile').click(function () {
		var agree=confirm("This will delete the current file!! \n Are you sure you want to delete the current file?");
		if (agree) {
		$('#currentFile').replaceWith('<input name="file" type="file"/>');
		$('#changeFile').replaceWith('');
		}
	});
	$('#message').hide();
	if (message.length > 0) {
		$('#message').show();
		$('#message').append(message);
		$('#message').fadeOut(4000);
	}
	$(".scrollTable tbody tr").mouseover(function(){$(this).addClass("over");}).mouseout(function(){$(this).removeClass("over");});
	$(".scrollTable tbody tr:even").addClass("alt");
	$(".scrollTable tbody tr:last").removeClass("alt");
	$(".scrollTable tbody tr:last").unbind();
	$('.addUser').click(function () {
		window.location = "users.php?newuser=yes";
	});
	$('#addGroup').click(function () {
		window.location = "meetings.php?page=group_edit&return_to=meetings.php?page=groups";
	});
	$('#addMeeting').click(function () {
		window.location = "meetings.php?page=meeting_edit&return_to=meetings.php";
	});
	$('#addMeetingType').click(function () {
		window.location = "meetings.php?page=types_edit&return_to=meetings.php?page=types";
	});
	$('#addLocation').click(function () {
		window.location = "meetings.php?page=location_edit&return_to=meetings.php?page=locations";
	});
});
// END JQuery document ready wrapper
		
//load the text editor options
function loadTinyMCE() {
	tinyMCE.init({
		// General options
		mode : "textareas",
		elements : "editHTML",
		theme : "advanced",
		width : "800px",
		height : "350px",
		plugins : "autolink,lists,pagebreak,style,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,searchreplace,contextmenu,paste,directionality,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
	
		// Theme options
		theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,bullist,numlist,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
        theme_advanced_buttons3 : "tablecontrols",
        table_styles : "Header 1=header1;Header 2=header2;Header 3=header3",
        table_cell_styles : "Header 1=header1;Header 2=header2;Header 3=header3;Table Cell=tableCel1",
        table_row_styles : "Header 1=header1;Header 2=header2;Header 3=header3;Table Row=tableRow1",
        table_cell_limit : 100,
        table_row_limit : 5,
        table_col_limit : 5,


		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_resizing : false,
		content_css : "../style/style.css",


		// Skin options
		skin : "default",
		skin_variant : "silver"
	});
	
}
//END load text editor

// Google Maps
var geocoder;
var map;
var starter_zoom=15;

function init()
{
    $("#map_canvas").empty();

    geocoder = new google.maps.Geocoder();
    var myOptions =
    {
        zoom: starter_zoom,
		streetViewControl: true,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}

function codeAddress(address,name,website)
{	init();
    geocoder.geocode( { 'address': address}, function(results, status)
    {
        if (status == google.maps.GeocoderStatus.OK)
        {
			var contentString = 
			'<div id="locattionInfo">'+
				'<div class="locMapInfoName">'+name+'</div>'+
				'<div class="locMapWebLink"><a href="'+website+'" title="'+website+'" target="_blank">'+website+'</a></div>'+
				'<div class="tofrom"><a href="http://maps.google.com/maps?daddr='+address+' ('+name+')&hl=en&ll='+results[0].geometry.location+'" title="To '+name+
				'" target="_blank">to here</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+
				'<a href="http://maps.google.com/maps?saddr='+address+' ('+name+')&hl=en&ll='+results[0].geometry.location+'" title="From '+name+
				'" target="_blank">from here</a></div>'
			'</div>';

			var infowindow = new google.maps.InfoWindow({
				content: contentString,
				maxWidth: "200px"
			});	
			
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        	});
			
			google.maps.event.addListener(marker, 'click', function() {
			infowindow.open(map,marker);
			});
        }
        else
        {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
}